OPC Studio User's Guide and Reference
Getting Started with OPC Wizard under .NET or .NET Framework using Visual Studio
Getting Started > Getting Started with OPC Wizard > Getting Started with OPC Wizard under .NET Framework or .NET > Getting Started with OPC Wizard under .NET or .NET Framework using Visual Studio
In This Topic

In this Getting Started, we will create an OPC UA server running in C# or VB.NET. The server will be hosted in a console application running in .NET Framework or .NET 6 or 8, and will provide a variable with dynamic random value.

Prerequisites

If you want to verify the version of .NET on your computer, use the dotnet --info command in the command prompt, and look for the Version field under ".NET runtimes installed:" in the generated output. Do not use dotnet --version, because this command only returns the version of the .NET command-line tools.

OPC UA Server in a Console Application

  1. Start the Visual Studio IDE (Integrated Development Environment).

  2. In this step, we will create a new .NET console application project.

    Select File -> New -> Project from the menu. The Create a new project dialog appears. In the language selection drop-down (initially set to All languages), select either C# or Visual Basic. Type Console App into the search box. Select either Console App (for .NET 6+) or Console App (.NET Framework).  Press the Next button. Go through the remaining steps of the wizard, making sure that you select a supported framework (see OPC Wizard Required .NET Runtimes).

  3. In this step, we will add a reference to the OpcLabs.OpcWizard NuGet package.

    Switch to the Solution Explorer window, right-click on the project node (ConsoleAppn), and select Manage NuGet Packages... command. In the NuGet: project window, switch to the Browse tab, and type OpcLabs.OpcWizard into the search box. Select the OpcLabs.OpcWizard package in the package list in the left pane of the window. In the right pane of the window, verify or change the package version next to the Version label. The version should be "Latest stable 5.81.build" (where build is a build number). Press the Install button.

  4. In the Solution Explorer window of Visual Studio, open the Program.cs (or Program.vb, if your are developing in VB.NET) file, and add following code to the beginning of the file:

    using OpcLabs.EasyOpc.UA;
    using OpcLabs.EasyOpc.UA.NodeSpace;
    
    Imports OpcLabs.EasyOpc.UA
    Imports OpcLabs.EasyOpc.UA.NodeSpace
    
  5. In Program.cs (or Program.vb, if your are developing in VB.NET), if there is a Main method, replace its body by the following code. If there is no Main method, just top-level statements, simply replace them all.

    var server = new EasyUAServer();
    var random = new Random();
    server.Add(new UADataVariable("DataVariable1").ReadValueFunction(() => random.Next()));
    
    server.Start(); 
    Console.ReadLine(); 
    server.Stop();
    
    Dim Server As New EasyUAServer()
    Dim Random As New Random()
    Server.Add(New UADataVariable("DataVariable1").ReadValueFunction(Function() Random.Next()))
    
    Server.Start()
    Console.ReadLine()
    Server.Stop()
    
  6. Select Run -> Start Debugging (F5) from the menu, or press the corresponding button on the toolbar.   

    This will build and launch the program. The server is now running. A firewall pop-up window may appear, asking you for consent with allowing the communication. Provide the consent as needed; allowing the communication on private networks is sufficient.

    OPC UA clients can connect to the server's endpoint on "opc.tcp://localhost:48040/" . You can verify it using any OPC UA client; see further below for instructions.

    After you are done, press Enter to exit the program.

Verification

When the OPC UA server is running, any OPC UA client can connect to it and read or subscribe to the value of the variable we have defined in the server. When the OPC UA client is on the same computer, it can connect to "opc.tcp://localhost:48040/" to access this server; otherwise, replace the "localhost" in the endpoint URL by the name of the computer (host) on your network.

If the OPC UA Local Discovery Server (LDS) is installed on the computer where the server is running, the server will automatically register itself with the LDS and will thus become discoverable by OPC UA clients through the LDS. For this to work, the LDS must either accept unauthenticated registrations, or be configured to trust the server. For more information, see OPC UA LDS Integration. The use of the LDS is optional.

If the OPC UA clients allows you to browse the address space of the server, the variable that we have defined is located directly under the Objects folder, and is named DataVariable1. Its node Id is:

Verification with the Connectivity Explorer

If you have the Connectivity Explorer application (e.g. if you have installed OPC Wizard using the OPC Studio Setup program, or obtained the Connectivity Explorer separately e.g. through ClickOnce deployment), you can use it for verification of the created OPC UA server as follows:

  1. Start the Connectivity Explorer application.
  2. In the Point Editor window, select Points (Composite) -> OPC Unified Architecture (Client-Server) Points -> Well-known -> opc.tcp://localhost:48040/ .
  3. Expand the selected node by clicking on the "+" mini-icon next to it, or by pressing + on the keyboard.
  4. Select the DataVariable1 node in the list view (middle pane) of the Point Editor window.
  5. Double-click on this DataVariable1 node, or select the "Add Live Data Row" in the Actions pane, or press Enter on the keyboard.
  6. Observe the variable value in the Live Point Data window.

Verification with the OpcCmd Utility

If you have the OpcCmd utility (e.g. if you have installed OPC Wizard using the OPC Studio Setup program, or obtained the OpcCmd separately e.g. through ClickOnce deployment), you can use it for verification of the created OPC UA server as follows:

  1. Start the OpcCmd utility.
  2. Enter the following command:
     
            uaClient subscribe opc.tcp://localhost:48040/ nsu=http://opclabs.com/OpcUA/Custom/Objects;s=DataVariable1
    
    
  3. Observe the variable value changes in the console output. The command will terminate automatically after 1 minute, or you can stop it by pressing X on the keyboard.

Security Notice

For simplicity in configuration, the OPC UA server created in this Getting Started exposes an insecure endpoint, and allows OPC UA connections without application authentication. Any production OPC UA server should, at least by default, disable the insecure endpoint. For more information, see Securing OPC Wizard Servers.

See Also

Fundamentals

Reference